package com.advaita.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class GetMacAddress {
	public String myMacAddress;

	public GetMacAddress() {
		try {
			InetAddress ip = InetAddress.getLocalHost();
			// System.out.println("Current IP address : " +
			// ip.getHostAddress());

			Enumeration<NetworkInterface> networks = NetworkInterface
					.getNetworkInterfaces();
			while (networks.hasMoreElements()) {
				NetworkInterface network = networks.nextElement();
				byte[] mac = network.getHardwareAddress();

				if (mac != null) {
					StringBuilder sb = new StringBuilder();
					for (int i = 0; i < mac.length; i++) {
						// sb.append(String.format("%02X%s", mac[i], (i <
						// mac.length - 1) ? "-" : ""));
						sb.append(String.format("%02X%s", mac[i],
								(i < mac.length - 1) ? "" : ""));
					}
					myMacAddress = sb.toString();
				}
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SocketException e) {
			e.printStackTrace();
		}
	} // end of constructor

	public static void main(String[] args) {

		GetMacAddress myMac = new GetMacAddress();
		System.out.println("in main: " + myMac.myMacAddress);
	}// end of main
}
